﻿using System;
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface;

namespace Ro.AssistFunction.ExtentReport
{
    /// <summary>
    /// 接口实现
    /// 可提交的LOG/Result
    /// TODO ExtentReport 更新至3.0.1，需要重新开发
    /// 仅从Testcase开始使用
    /// </summary>
    public class RoExtentReport : IRoExtentReport
    {
        /// <summary>
        /// ExtentTest主体
        /// </summary>
        private static ExtentTest _depExtentTest;


        /// <summary>
        /// 初始化ExtentReport
        /// </summary>
        /// <param name="filename">文件名称</param>
        public void InitExtentReports(string filename)
        {
            string nowtime = DateTime.Now.ToString("yyyy-MM-dd");
            string exepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            //直接保存在当前
            string htmllog = RoPubArgments.TempLogInfo.FolderPath + "/" + nowtime + "-" + filename + ".html";

            //初始化
            ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(htmllog);
            RoPubArgments.RoExtentReports = new ExtentReports();
            //创建Log
            RoPubArgments.RoExtentReports.AttachReporter(htmlReporter);
            //设置，不在通过config文件
            htmlReporter.LoadConfig(exepath + "ExtentReport.config");
        }

        /// <summary>
        /// 开始测试记录
        /// 在单个测试Case中执行一次
        /// </summary>
        /// <param name="casename">测试名称</param>
        /// <param name="testdescription">测试描述</param>
        public void StartExtentReports(string casename, string testdescription)
        {
            _depExtentTest = RoPubArgments.RoExtentReports.CreateTest(casename, testdescription);
        }

        /// <summary>
        /// 结束测试
        /// 所有Case运行结束后执行
        /// </summary>
        public void EndExtentReports()
        {
            RoPubArgments.RoExtentReports.Flush();
        }

        /// <summary>
        /// 添加测试子节点的信息，对每个step执行
        /// </summary>
        /// <param name="logStatus">测试结果</param>
        /// <param name="stepname">步骤名称</param>
        /// <param name="stepdetail">测试详情</param>
        public void AddTestsChildNode(Status logStatus, string stepname, string stepdetail)
        {
            //创建一个测试结果节点
            ExtentTest child = _depExtentTest.CreateNode(stepname);
            BackNewExtentTest(child, logStatus, stepdetail);
        }

        /// <summary>
        /// 根据传入的Status进行重新返回
        /// 普通详情
        /// </summary>
        /// <param name="childTest"></param>
        /// <param name="status"></param>
        /// <param name="details"></param>
        /// <returns></returns>
        private void BackNewExtentTest(ExtentTest childTest, Status status, string details)
        {
            switch (status)
            {
                case Status.Pass:
                    childTest.Pass(details);
                    break;
                case Status.Fail:
                    childTest.Fail(details);
                    break;
                case Status.Fatal:
                    childTest.Fatal(details);
                    break;
                case Status.Debug:
                    childTest.Debug(details);
                    break;
                case Status.Error:
                    childTest.Error(details);
                    break;
                case Status.Info:
                    childTest.Info(details);
                    break;
                case Status.Skip:
                    childTest.Skip(details);
                    break;
                case Status.Warning:
                    childTest.Warning(details);
                    break;
            }
          
        }

        /// <summary>
        /// 添加测试结果
        /// </summary>
        /// <param name="logStatus">测试结果true正确 false是fail</param>
        /// <param name="stepdetail">测试详情</param>
        public void AddTestsResult(bool logStatus, string stepdetail)
        {
            if (logStatus)
            {
                _depExtentTest.Pass(stepdetail);
            }
            else
            {
                _depExtentTest.Fail(stepdetail);
            }
        }
    }
}
